/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kz.epam.creator;

import java.util.HashMap;
import kz.epam.homedevices.DeviceKind;
import kz.epam.homedevices.ElectricalDevice;
import kz.epam.homedevices.Machining.MachiningDevice;
import kz.epam.homedevices.Machining.MachiningType;
import kz.epam.homedevices.communication.CommunicationDevice;
import kz.epam.homedevices.communication.CommunicationType;
import kz.epam.homedevices.computing.ComputingDevice;
import kz.epam.homedevices.computing.ComputingType;
import kz.epam.homedevices.instrumentation.MeasuringDevice;
import kz.epam.homedevices.instrumentation.MeasuringType;
import kz.epam.homedevices.lighting.Lighting;
import kz.epam.homedevices.lighting.LightingType;
import kz.epam.homedevices.storageproducts.StorageProductsDevice;
import kz.epam.homedevices.storageproducts.StorageProductsType;
import kz.epam.homedevices.videobroadcasting.VideoBroadcastingDevice;
import kz.epam.homedevices.videobroadcasting.VideoBroadcastingType;

/**
 *
 * @author Knoppix
 */
// Building devices for properties
public class DeviceBuilder {

    private DeviceKind type;

    public DeviceBuilder() {
    }

    public DeviceBuilder(DeviceKind type) {
        this.type = type;
    }

    public DeviceKind getType() {
        return type;
    }

    public void setType(DeviceKind type) {
        this.type = type;
    }

    public ElectricalDevice create(HashMap<String, String> properties) {
        ElectricalDevice resultDevice = null;
        switch (type) {
            case COMMUNICATION:
                return createCommumicationDevice(properties);
            case MACHINING:
                return createMachiningDevice(properties);
            case COMPUTING:
                return createComputingDevice(properties);
            case INSTRUMENTATION:
                return createMeasuringDevice(properties);
            case VIDEO_BROADCASTING:
                return createVideoBroadcastingDevice(properties);
            case STORAGE_PRODUCTS:
                return createStorageProductsDevice(properties);
            case LIGHTING:
                return createLighting(properties);
        }
        return resultDevice;
    }

    private CommunicationDevice createCommumicationDevice(HashMap<String, String> properties) {
        CommunicationDevice resultDevice = new CommunicationDevice();
        resultDevice.setType(CommunicationType.valueOf(properties.get("type")));
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setNumber(properties.get("number"));
        return resultDevice;
    }

    private ElectricalDevice createMachiningDevice(HashMap<String, String> properties) {
        MachiningDevice resultDevice = new MachiningDevice();
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setFrequency(Integer.parseInt(properties.get("frequency")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setType(MachiningType.valueOf(properties.get("type")));
        return resultDevice;
    }

    private ElectricalDevice createComputingDevice(HashMap<String, String> properties) {
        ComputingDevice resultDevice = new ComputingDevice();
        resultDevice.setType(ComputingType.valueOf(properties.get("type")));
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setCpuSpeed(Integer.parseInt(properties.get("cpuSpeed")));
        return resultDevice;
    }

    private ElectricalDevice createMeasuringDevice(HashMap<String, String> properties) {
        MeasuringDevice resultDevice = new MeasuringDevice();
        resultDevice.setType(MeasuringType.valueOf(properties.get("type")));
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setValue(Double.parseDouble(properties.get("value")));
        return resultDevice;
    }

    private ElectricalDevice createVideoBroadcastingDevice(HashMap<String, String> properties) {
        VideoBroadcastingDevice resultDevice = new VideoBroadcastingDevice();
        resultDevice.setType(VideoBroadcastingType.valueOf(properties.get("type")));
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setScreenDiagonal(Integer.parseInt(properties.get("screenDiagonal")));
        return resultDevice;
    }

    private ElectricalDevice createStorageProductsDevice(HashMap<String, String> properties) {
        StorageProductsDevice resultDevice = new StorageProductsDevice();
        resultDevice.setType(StorageProductsType.valueOf(properties.get("type")));
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setTemperature(Integer.parseInt(properties.get("temperature")));
        return resultDevice;
    }

    private ElectricalDevice createLighting(HashMap<String, String> properties) {
        Lighting resultDevice = new Lighting();
        resultDevice.setType(LightingType.valueOf(properties.get("type")));
        resultDevice.setMaker(properties.get("maker"));
        resultDevice.setCardinality(Double.parseDouble(properties.get("cardinality")));
        resultDevice.setPowerOn(Boolean.parseBoolean(properties.get("powerOn")));
        resultDevice.setLuminousEfficiency(Double.parseDouble(properties.get("luminousEfficiency")));
        return resultDevice;
    }
}
